package com.zut.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.dto.SetmealDto;
import com.zut.pojo.Category;
import com.zut.pojo.Dish;
import com.zut.pojo.R;
import com.zut.pojo.Setmeal;
import com.zut.service.CategoryService;
import com.zut.service.SetmealDishService;
import com.zut.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /*
    *
    * 新增套餐
    *
    * */

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }


    /*
    *
    * 分页查询
    * 条件查询
    *
    * */


    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){

        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);


        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);


        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        /*
        * 分页查询显示套餐分类名称
        * */

        //构建一个包含套餐分类名称的新的分页构造器（setmealdto)
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //将pageinfo中的分页对象拷贝到新的分页构造器
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        //拿到pageinfo中的records对象（records：分页查询页面展示出来的数据的集合）
        List<Setmeal> records = pageInfo.getRecords();

        /*用stream流将records集合里面的每个元素（元素为每个setmeal实体类对象）
        拷贝到setmealdto实体类对象中（因为setmealdto中有categoryname属性，对应页面套餐分类名称）*/
        List<SetmealDto> list = records.stream().map((record) -> {

            //new一个setmealdto对象
            SetmealDto setmealDto = new SetmealDto();
            //拷贝
            BeanUtils.copyProperties(record, setmealDto);
            //获取套餐分类名称
            Long categoryId = setmealDto.getCategoryId();  //分类id
            Category category = categoryService.getById(categoryId);//根据id查询分类对象

            //判断分类对象是否为空
            if (category != null) {
                //不为空
                String categoryName = category.getName();  //根据分类对象获取套餐分类名称
                setmealDto.setCategoryName(categoryName);  //修改setmealdto中的categoryname属性

            }

            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);


        return R.success(setmealDtoPage);


    }


    /*
    *
    * 批量删除
    *
    * */

    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        log.info("删除套餐，id为：{}",ids);
        setmealService.removeByIds(ids);

        return R.success("删除套餐成功");
    }

    /*
     *
     * 批量停售/起售
     * */

    @PostMapping("/status/{status}")
    public R<String> updatestatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        log.info("停售/起售的菜品，id为，{}",ids);
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus,status).in(Setmeal::getId,ids);

        setmealService.update(updateWrapper);

        return R.success("批量起售和停售套餐成功");
    }



}
